﻿namespace DataServer.ApplicationLib.Rules
{
    public class ConfigDataRuleFactory
    {
        public static IConfigDataRule CreateConfigDataRule(ConfigDataCreateRuleRequest request,
            RuleContext ruleContext)
        {
            if (ruleContext == null)
                throw new ArgumentNullException(nameof(ruleContext));

            if (ruleContext.IsValid)
            {
                var configDataRule = new ConfigDataRuleDefault(ruleContext);
                // 数据写入Check表:用ODP的BulkCopy，需先生成ID和Request_ID
                //ConfigDataRuleBase rule = new ConfigDataInitRule(context);

                if (request.EnableFieldRule)
                {
                    configDataRule.SetSuccessor(new ConfigDataRuleField(ruleContext));
                    // rule.SetSuccessor(new ConfigDataFieldRule(context));
                }

                if (request.EnableRowRule)
                {
                    configDataRule.SetSuccessor(new ConfigDataRuleRow(ruleContext));
                    // rule.SetSuccessor(new ConfigDataRowRule(context));
                }

                if (request.EnablePrimaryKeyRule)
                {
                    configDataRule.SetSuccessor(new ConfigDataRuleKey(ruleContext));
                    // rule.SetSuccessor(new ConfigDataKeyRule(context));
                }

                //if (request.EnableTableRule)
                //{
                //    configDataRule.SetSuccessor(new ConfigDataRuleTable(ruleContext));
                //}

                //if (request.EnableRoleRule)
                //{
                //    configDataRule.SetSuccessor(new ConfigDataRoleRule(context));
                //}

                var decorator = new VerifySuccessDecorator(ruleContext);
                decorator.SetDataRule(configDataRule);

                return decorator;
            }
            else
            {
                throw new InvalidOperationException(nameof(ruleContext.IsValid));
            }
        }
    }
}
